/**
 * 标准化参数
 */

const fs = require('fs'),
  chalk = require('chalk'),
  path = require('path'),
  paths = require('./paths')

function normalizationArgv(argv) {
  let app = argv['_'][0]

  if (!fs.existsSync(path.join(paths.cwd, app))) {
    // 处理名称
    const appName = fs.readdirSync(paths.cwd).find(dirname => {
      return fs.existsSync(path.join(paths.cwd, dirname, 'package.json')) && new RegExp(`${app}$`).test(dirname)
    })
    app = appName
  }

  if (!app) {
    console.log(chalk.red('没有需要编译的应用: 请使用 yarn build [appname]'))
    process.exit(1)
  }

  return {
    app
  }
}

module.exports = {
  normalizationArgv
}